//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 15
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace IndexedPageView
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            IndexedPageView(binding);
        }

        /// <summary>
        /// Issue an IndexedPageView FindFolder request (Listing 15-2)
        /// </summary>
        /// <param name="binding">binding to use for call</param>
        /// 
        public static void IndexedPageView(ExchangeServiceBinding binding)
        {
            FindFolderType request = new FindFolderType();
            request.FolderShape = new FolderResponseShapeType();
            request.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
            // add the additional property
            //
            PathToUnindexedFieldType displayNamePath = new PathToUnindexedFieldType();
            displayNamePath.FieldURI = UnindexedFieldURIType.folderDisplayName;
            request.FolderShape.AdditionalProperties = new BasePathToElementType[] { displayNamePath };
            // Set up the paging
            //
            IndexedPageViewType paging = new IndexedPageViewType();
            paging.BasePoint = IndexBasePointType.Beginning;
            paging.MaxEntriesReturned = 10;
            paging.MaxEntriesReturnedSpecified = true;
            paging.Offset = 10;
            // Yes, this is weird. Set the paging on the Item property.
            //
            request.Item = paging;
            // Parent folders to search from
            //
            DistinguishedFolderIdType inboxId = new DistinguishedFolderIdType();
            inboxId.Id = DistinguishedFolderIdNameType.inbox;
            request.ParentFolderIds = new BaseFolderIdType[] { inboxId };
            request.Traversal = FolderQueryTraversalType.Shallow;
            FindFolderResponseType response = binding.FindFolder(request);
            FindFolderResponseMessageType responseMessage =
            response.ResponseMessages.Items[0] as FindFolderResponseMessageType;
            Console.WriteLine("Our paging results:");
            Console.WriteLine("Included last item: {0}",
            responseMessage.RootFolder.IncludesLastItemInRange);
            Console.WriteLine("Next index to fetch: {0}",
            responseMessage.RootFolder.IndexedPagingOffset);
            Console.WriteLine("Total items that can be returned: {0}",
            responseMessage.RootFolder.TotalItemsInView);
            Console.WriteLine("Items returned in THIS page: {0}",
            responseMessage.RootFolder.Folders.Length);
        }
    }
}
